package com.marvels.approve.consum.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONObject;
import com.marvels.approve.consum.ApproveConsumerFinancialApi;
import com.marvels.approve.loan.dto.FundSourceDTO;
import com.marvels.approve.loan.dto.LoanAfterInfoDTO;
import com.marvels.approve.loan.dto.McfeLendingProcess;
import com.marvels.approve.loan.dto.McfeLendingProcessLog;
import com.marvels.approve.manager.dto.ApprovalProcessConditionDTO;
import com.marvels.approve.manager.dto.CollectAccountDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.api.LoanAccountApi;
import com.marvels.financial.loan.api.FiancialLoanAuditApi;
import com.marvels.financial.loan.api.FinancialFundSourceApi;
import com.marvels.financial.loan.api.FinancialILoanAfterInfoApi;

import cn.hutool.core.bean.BeanUtil;

@Component
public class ApproveConsumerFinancialImpl implements ApproveConsumerFinancialApi{

	@Autowired
	private FinancialFundSourceApi financialFundSourceService;
	
	@Autowired
	private FiancialLoanAuditApi fiancialLoanAuditApi;
	
	@Autowired
	private FinancialILoanAfterInfoApi loanAfterInfoApi;
	
	@Autowired
	private LoanAccountApi loanAccountApi;
	
	@Override
	public List<FundSourceDTO> fundSourceInfo(@RequestBody FundSourceDTO fundDTO) {
		try {
			com.marvels.financial.funds.dto.FundDTO param = new com.marvels.financial.funds.dto.FundDTO();
			BeanUtil.copyProperties(fundDTO, param);
			List<com.marvels.financial.funds.dto.FundDTO> apiResult = financialFundSourceService.fundSourceInfo(param);
			List<FundSourceDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), FundSourceDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.fundSourceInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.fundSourceInfo error", e);
			return null;
		}
	}

	@Override
	public int insertLoanAuditInfo(@RequestBody McfeLendingProcess mcfeLendingProcess) {
		try {
			com.marvels.financial.loan.dto.McfeLendingProcess param = new com.marvels.financial.loan.dto.McfeLendingProcess();
			BeanUtil.copyProperties(mcfeLendingProcess, param);
			int result = fiancialLoanAuditApi.insertLoanAuditInfo(param);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.insertLoanAuditInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.insertLoanAuditInfo error", e);
			return 0;
		}
	}

	@Override
	public McfeLendingProcess selectByloanIdAndStatus(String loanId, String status) {
		try {
			com.marvels.financial.loan.dto.McfeLendingProcess apiResult = fiancialLoanAuditApi.selectByloanIdAndStatus(loanId, status);
			McfeLendingProcess result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McfeLendingProcess.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.selectByloanIdAndStatus direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.selectByloanIdAndStatus error", e);
			return null;
		}
	}

	@Override
	public int insertLoanAuditLogInfo(@RequestBody  McfeLendingProcessLog mcfeLendingProcessLog) {
		try {
			com.marvels.financial.loan.dto.McfeLendingProcessLog param = new com.marvels.financial.loan.dto.McfeLendingProcessLog();
			BeanUtil.copyProperties(mcfeLendingProcessLog, param);
			int result = fiancialLoanAuditApi.insertLoanAuditLogInfo(param);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.insertLoanAuditLogInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.insertLoanAuditLogInfo error", e);
			return 0;
		}
	}

	@Override
	public LoanAfterInfoDTO loanTrial(LoanAfterInfoDTO loanAfterInfoDTO) {
		try {
			com.marvels.financial.loan.dto.LoanAfterInfoDTO param = new com.marvels.financial.loan.dto.LoanAfterInfoDTO(); 
			BeanUtil.copyProperties(loanAfterInfoDTO, param);
			com.marvels.financial.loan.dto.LoanAfterInfoDTO apiResult = loanAfterInfoApi.loanTrial(param);
			LoanAfterInfoDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), LoanAfterInfoDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.loanTrial direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.loanTrial error", e);
			return null;
		}
	}

	@Override
	public List<String> overdueLoanList(ApprovalProcessConditionDTO dto) {
		try {
			com.marvels.financial.loan.dto.ApprovalProcessConditionDTO param = new com.marvels.financial.loan.dto.ApprovalProcessConditionDTO();
			BeanUtil.copyProperties(dto, param);
			List<String> result = loanAfterInfoApi.overdueLoanList(param);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.overdueLoanList direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.overdueLoanList error", e);
			return null;
		}
	}

	@Override
	public CollectAccountDTO selectAccountInfo(String loanId) throws MarvelsBusinessException {
		try {
			com.marvels.financial.collection.dto.CollectAccountDTO apiResult = loanAccountApi.selectAccountInfo(loanId);
			CollectAccountDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), CollectAccountDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.selectAccountInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.selectAccountInfo error", e);
			return null;
		}
	}
	
	@Override
	public Map<String, String> selectAccountOverdueInfo(List<String> loanIds) {
		try {
			Map<String, String> result = loanAccountApi.selectAccountOverdueInfo(loanIds);
			MarvelsLogUtil.getInstance().info("ApproveConsumerFinancialApi.selectAccountOverdueInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerFinancialApi.selectAccountOverdueInfo error", e);
			return null;
		}
	}
}
